package com.zmn.plat.admin.controller.message;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.message.MsgWechatTemplateBService;
import com.zmn.plat.manager.constant.MsgTemplateConsts;
import com.zmn.plat.manager.dictionary.MsgTemplateDict;
import com.zmn.plat.model.entity.message.*;
import com.zmn.plat.model.vo.message.MsgWechatAppsVO;
import com.zmn.plat.services.interfaces.message.*;

/**
 * 描述: 消息管理查询Controller
 *
 * @auther tanghongjun
 * @since 2019/10/21 16:36
 */
@Controller
@RequestMapping("/msg/temp/")
public class MsgTemplateListController extends PlatBaseController {

    @Autowired
    private MsgTemplateService msgTemplateService;

    @Autowired
    MsgWechatTemplateService msgWechatTemplateService;

    @Autowired
    MsgWechatTemplateBService msgWechatTemplateBService;

    @Autowired
    MsgSmsTemplateService msgSmsTemplateService;

    @Autowired
    MsgAliTemplateService msgAliTemplateService;

    @Autowired
    MsgBaiduTemplateService msgBaiduTemplateService;

    /**
     * 模板管理
     *
     * @param query
     *            the query
     * @return the model and view
     * @author xiangjunming
     * @since 2019/10/23
     */
    @RequestMapping(value = "listByGroupId")
    @RequiresPermissions("/msg/temp/listByGroupId.action")
    @ReadOnlyConnection
    public ModelAndView listByGroupId(MsgTemplateQuery query) {

        ModelAndView mav = new ModelAndView("message/list");

        if (Objects.nonNull(query.getCreateTimeRange()) && !Objects.equals(query.getCreateTimeRange(), "")) {
            String[] createTimeRange = query.getCreateTimeRange().split(" - ");
            query.setCreateTimeBegin(DateUtil.toString(DateUtil.getDateStart(DateUtil.parse(createTimeRange[0], DateUtil.FORMAT_DATE3))));
            query
                .setCreateTimeEnd(DateUtil.toString(DateUtil.getDateEnd(DateUtil.parse(createTimeRange[1].trim(), DateUtil.FORMAT_DATE3))));
        }

        List<MsgTemplate> msgTemplates = msgTemplateService.listPageByQuery(query);

        mav.addObject("items", msgTemplates);
        mav.addObject("vo", query);
        mav.addObject("types", MsgTemplateDict.getTempTypelist());
        mav.addObject("statusList", MsgTemplateDict.getStatuslist());
        return mav;
    }

    /**
     * 新增或修改页面
     *
     * @param groupId
     * @param templateId
     * @return
     * @author xiangjunming
     * @since 2019/10/23
     */
    @RequestMapping(value = "detail")
    @RequiresPermissions("/msg/temp/detail.action")
    @ReadOnlyConnection
    public ModelAndView detail(Integer groupId, Integer templateId) {
        ModelAndView mav = new ModelAndView("message/detail");
        MsgTemplate obj = null;
        if (templateId == null || templateId == 0) {
            obj = new MsgTemplate();
            obj.setGroupId(groupId);
        } else {
            obj = msgTemplateService.findByKey(templateId);
        }
        mav.addObject("obj", obj == null ? new MsgTemplate() : obj);
        mav.addObject("types", MsgTemplateDict.getTempTypelist());
        mav.addObject("flag", 1);
        return mav;
    }

    /**
     * 消息内容页面
     *
     * @param templateId
     * @return
     * @author xiangjunming
     * @since 2019/10/23
     */
    @RequestMapping(value = "content")
    @RequiresPermissions("/msg/temp/detail.action")
    @ReadOnlyConnection
    public ModelAndView content(Integer templateId, @RequestParam(value = "type", defaultValue = "1") Integer type) {

        ModelAndView mav = new ModelAndView("message/content");
        MsgTemplate obj = msgTemplateService.findByKey(templateId);
        mav.addObject("obj", obj);
        mav.addObject("flag", 2);
        mav.addObject("type", type);

        if (type == MsgTemplateConsts.MSG_TYPE_MSG) {
            // 查询短信配置
            MsgSmsTemplate smsTemplate =
                Optional.ofNullable(msgSmsTemplateService.findByTemplateId(templateId)).orElse(new MsgSmsTemplate());
            mav.addObject("smsTemplate", smsTemplate);
        }
        if (Objects.equals(type, MsgTemplateConsts.MSG_TYPE_WECHAT)) {
            // 查询微信配置
            // ResponseDTO<List<MsgWechatAppsVO>> response = msgWechatTemplateBService.listWeChatAppIdAndNames();
            List<MsgWechatAppsVO> wechatApps = msgWechatTemplateBService.listWeChatAppIdAndNames();
            // if (response.isSuccess()) {
            //     wechatApps = response.getData();
            // } else {
            //     wechatApps = Collections.emptyList();
            // }
            mav.addObject("wechatApps", wechatApps);
            List<MsgWechatTemplate> msgWechatTemplates = msgWechatTemplateService.listByTemplateId(templateId);
            mav.addObject("wechats", msgWechatTemplates);
        }
        if (Objects.equals(type, MsgTemplateConsts.MSG_TYPE_ZFB)) {
            // 查询支付宝配置
            MsgAliTemplate aliTemplate = msgAliTemplateService.findByTemplateId(templateId);
            mav.addObject("aliTemplate", Optional.ofNullable(aliTemplate).orElse(new MsgAliTemplate()));
        }
        if (Objects.equals(type, MsgTemplateConsts.MSG_TYPE_BAIDU)) {
            MsgBaiduTemplate baiduTemplate = msgBaiduTemplateService.findByTemplateId(templateId);
            mav.addObject("baiduTemplate", Optional.ofNullable(baiduTemplate).orElse(new MsgBaiduTemplate()));
            mav.addObject("sceneType", MsgTemplateDict.listSceneType());
        }
        return mav;
    }
}
